package
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	/** A menu of tools to be used in the garden */
	public class GardenTools extends Sprite
	{
		public static const OUTLINE_COLOR:uint = 0x000000, BACKGROUND_COLOR:uint = 0xCC6666, TITLE_BACKGROUND_COLOR:uint=0886666;
		public static const DEFAULT_TEXT_COLOR:uint = 0x4444AA;
		public var WIDTH:Number, HEIGHT:Number, TITLE_HEIGHT:Number;
		public var BUTTON_WIDTH:Number = 80;
		public var enabled:Boolean = true;
		public var modeName:String="";
		
		// private
		private static const X_PADDING:int = 8, Y_PADDING:int = 4;
		private var selectedButton:Sprite;
		private var selectedOutline:Shape, overOutline:Shape;
		private var penb:Sprite, colorb:Sprite, deleteb:Sprite; 
		private var hide_pen:Boolean = false, hide_color:Boolean = false, hide_delete:Boolean = false;
		private var toolTField:TextField, penTField:TextField, colorTField:TextField, deleteTField:TextField; 
		private var unit:Number;
		private var initial_tool:String;
		
		public function GardenTools(WIDTH:Number, HEIGHT:Number, TITLE_HEIGHT:Number, initial_tool:String="color")
		{
			this.WIDTH = WIDTH;
			this.HEIGHT = HEIGHT;
			this.TITLE_HEIGHT = TITLE_HEIGHT;
			this.initial_tool = initial_tool;
			this.addEventListener (Event.ADDED, handleAdded);
		}
		private function handleAdded (event:Event):void
		{
			unit = stage.width/1000;
			this.removeEventListener (Event.ADDED, handleAdded);
			
			BUTTON_WIDTH = Math.min(WIDTH/2 - X_PADDING, HEIGHT/4);
			
			//this.graphics.lineStyle(unit*1, OUTLINE_COLOR);
			this.graphics.beginFill(TITLE_BACKGROUND_COLOR);
			this.graphics.drawRect(0,0, WIDTH, TITLE_HEIGHT);
			this.graphics.endFill();
			this.graphics.lineStyle(unit*1, 0xFFFFFF);
			this.graphics.moveTo(0,TITLE_HEIGHT);
			this.graphics.lineTo(WIDTH,TITLE_HEIGHT);
			
			// draw title
			toolTField = new TextField ();
			toolTField.mouseEnabled = false;
			toolTField.selectable = false;
			toolTField.defaultTextFormat = new TextFormat ("arial", TITLE_HEIGHT*2/3*unit, 0xFFFFFF, null, null, null, null, null, "center");
			toolTField.text = "Tools";
			toolTField.autoSize = "center";
			toolTField.x = (this.width - toolTField.width)/2;
			toolTField.y = 0;
			this.addChild(toolTField);			
			
			//this.graphics.lineStyle(unit*1, OUTLINE_COLOR);
			this.graphics.beginFill(BACKGROUND_COLOR);
			this.graphics.drawRect(0,TITLE_HEIGHT, WIDTH, HEIGHT-TITLE_HEIGHT);
			this.graphics.endFill();
			
			
			// draw buttons
			if (!hide_color)
			{
				drawColorButton();
				colorb.x = X_PADDING;
				colorb.y = TITLE_HEIGHT + Y_PADDING;
				colorTField = new TextField();
				colorTField.selectable = false;
				colorTField.defaultTextFormat = new TextFormat("arial", 18*unit, DEFAULT_TEXT_COLOR);
				colorTField.text = "color";
				colorTField.autoSize = "left";
				colorTField.x = colorb.x + colorb.width + (colorb.width - colorTField.width)/2;
				colorTField.y = colorb.y + (colorb.height-colorTField.height)/2;// + PADDING;
				this.addChild(colorTField);
			}
			if (!hide_pen)
			{
				drawPenButton();
				penb.x = X_PADDING;
				penb.y = colorb.y + colorb.height + Y_PADDING;
				penTField = new TextField();
				penTField.selectable = false;
				penTField.defaultTextFormat = new TextFormat("arial", 18*unit, DEFAULT_TEXT_COLOR);
				penTField.text = "pen";
				penTField.autoSize = "left";
				penTField.x = penb.x + penb.width + (penb.width - penTField.width)/2;
				penTField.y = penb.y + (penb.height-penTField.height)/2;// + PADDING;
				this.addChild(penTField);
			}
			
			
			if (!hide_delete)
			{	
				drawDeleteButton();
				deleteb.x = X_PADDING;
				deleteb.y = penb.y + penb.height + Y_PADDING;
				deleteTField = new TextField();
				deleteTField.selectable = false;
				deleteTField.defaultTextFormat = new TextFormat("arial", 18*unit, DEFAULT_TEXT_COLOR);
				deleteTField.text = "delete";
				deleteTField.autoSize = "left";
				deleteTField.x = deleteb.x + deleteb.width + (deleteb.width - deleteTField.width)/2;
				deleteTField.y = deleteb.y + (deleteb.height-deleteTField.height)/2;// + PADDING;
				this.addChild(deleteTField);
			}
			if (initial_tool == "color")
			{
				// initialize with color
				if (selectedOutline == null)
				{
					drawSelectedOutline();
				}
				// update references
				selectedOutline.x = colorb.x;
				selectedOutline.y = colorb.y;
				selectedButton = colorb;
				modeName = selectedButton.name;
			}
			
		}
		
		/// for the tool buttons
		private function handleOverButton (event:MouseEvent):void
		{
			if (enabled)
			{
				// create outline if not made yet
				if (overOutline == null)
				{
					overOutline = new Shape();
					this.addChild(overOutline);
				} else
				{
					overOutline.graphics.clear();
				}			
				overOutline.graphics.lineStyle(2,0x00000);
				overOutline.graphics.drawRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH);
				
				overOutline.x = event.currentTarget.x;
				overOutline.y = event.currentTarget.y;
				// add a click handler
				event.currentTarget.addEventListener (MouseEvent.CLICK, handleClickButton);
			}
		}
		private function handleClickButton (event:MouseEvent):void
		{
			if (selectedButton != event.currentTarget)
			{
				
				if (selectedOutline == null)
				{
					drawSelectedOutline();
				}
				// update references
				selectedOutline.x = event.currentTarget.x;
				selectedOutline.y = event.currentTarget.y;
				selectedButton = Sprite(event.currentTarget);
				modeName = event.currentTarget.name;
				
				// call to parent function
				this.dispatchEvent( new Event(Event.COMPLETE)); // lets parent know that we have selected a new tool
			}
		}
		
		private function drawColorButton():Sprite
		{
			var cursor:Sprite;
			if (colorb == null)
			{
				colorb = new Sprite();
				colorb.name = "color";
				colorb.addEventListener(MouseEvent.MOUSE_OVER, handleOverButton);
				this.addChild(colorb);
				cursor = new Sprite();
				cursor.mouseEnabled = false;
				colorb.addChild(cursor);
			} else 
			{
				colorb.graphics.clear();
			}
			//var pointsize:Number = 30*unit;
			//var cdenom:Number = 1*unit;
			//var adenom:Number = 4*unit;
			var h:Number = unit*6; 
			colorb.graphics.lineStyle(1*unit, 0xAA8866);
			colorb.graphics.beginFill(0xFFEEBB);
			colorb.graphics.drawRect(0,0,BUTTON_WIDTH, BUTTON_WIDTH);
			colorb.graphics.endFill();
			cursor.x = BUTTON_WIDTH/2;
			cursor.y = BUTTON_WIDTH/2+4*unit;
			cursor.graphics.lineStyle(h/10, 0xAA8866);
			var cursor_color:uint = 0xFFAAAA;
			cursor.graphics.beginFill(cursor_color);
			// draw dropper
			cursor.graphics.moveTo(h/8, 0);
			cursor.graphics.lineTo(h/4, -h/2);
			cursor.graphics.lineTo(h/4, -3*h);
			cursor.graphics.lineTo(-h/4, -3*h);
			cursor.graphics.lineTo(-h/4, -h/2);
			cursor.graphics.lineTo(-h/8, 0);
			cursor.graphics.lineStyle(h/8, 0xAA8866, .5);
			cursor.graphics.lineTo(h/8, 0);
			cursor.graphics.endFill();
			cursor.graphics.beginFill(0xAAAAAA);
			cursor.graphics.drawCircle(0, -3*h, h/3);
			cursor.graphics.endFill();
			cursor.rotation = 30;
			
			return colorb;
		}
		private function drawPenButton():Sprite
		{
			var cursor:Sprite;
			if (penb == null)
			{
				penb = new Sprite();
				penb.name = "pen";
				penb.addEventListener(MouseEvent.MOUSE_OVER, handleOverButton);
				this.addChild(penb);
				cursor = new Sprite();
				cursor.mouseEnabled = false;
				penb.addChild(cursor);
				
			} else 
			{
				penb.graphics.clear();
				cursor = Sprite(penb.getChildAt(0));	
				cursor.graphics.clear();
			}
			var pwidth:Number = 4;
			var pheight:Number = 20;
			penb.graphics.lineStyle(1*unit, 0xAA8866);
			penb.graphics.beginFill(0xFFEEBB);
			penb.graphics.drawRect(0,0,BUTTON_WIDTH, BUTTON_WIDTH);
			penb.graphics.endFill();
			var pointsize:Number = 30*unit;
			cursor.x = BUTTON_WIDTH/2;
			cursor.y = BUTTON_WIDTH/2+4*unit;
			// draw circle
			var cx:Number = 0;
			var cy:Number = 0;
			cursor.graphics.lineStyle(pwidth/4,0x888888);
			cursor.graphics.beginFill(0xFFFF00);
			//cursor.graphics.beginFill(0x888888);
			cursor.graphics.drawEllipse(cx-5*unit,cy-5*unit,10*unit,10*unit);
			cursor.graphics.endFill();
			// draw pen
			cursor.graphics.lineStyle(pwidth/8,0x444444);
			cursor.graphics.beginFill(0x888888);
			cursor.graphics.moveTo(cx,cy);
			cursor.graphics.lineTo(cx+pwidth/2,cy-pwidth);
			cursor.graphics.lineTo(cx-pwidth/2,cy-pwidth)
			cursor.graphics.lineTo(cx,cy);
			cursor.graphics.beginFill(0x8888FF);
			cursor.graphics.moveTo(cx+pwidth/2,cy-pwidth);
			cursor.graphics.lineTo(cx+pwidth/2,cy-pheight);
			cursor.graphics.lineTo(cx-pwidth/2,cy-pheight);
			cursor.graphics.lineTo(cx-pwidth/2,cy-pwidth)
			cursor.graphics.endFill();
			// handle
			cursor.graphics.beginFill(0x888888);
			cursor.graphics.drawEllipse(cx-pwidth/2,cy-pheight-pwidth/4,pwidth,pwidth/2);
			cursor.graphics.endFill();
			cursor.rotation=30;
			
			return penb;
		}
		private function drawDeleteButton():Sprite
		{
			if (deleteb == null)
			{
				deleteb = new Sprite();
				deleteb.name = "delete";
				deleteb.addEventListener(MouseEvent.MOUSE_OVER, handleOverButton);
				this.addChild(deleteb);
			} else 
			{
				deleteb.graphics.clear();
			}
			deleteb.graphics.lineStyle(1*unit, 0xAA8866);
			deleteb.graphics.beginFill(0xFFEEBB);
			deleteb.graphics.drawRect(0,0,BUTTON_WIDTH, BUTTON_WIDTH);
			deleteb.graphics.endFill();
			var cursor:Shape = new Shape();
			var erasersize:Number = 10*unit;
			cursor.graphics.clear();
			// draw circle
			cursor.graphics.beginFill(0xCCCCCC);
			cursor.graphics.drawEllipse(-5*unit,-5*unit,10*unit,10*unit);
			cursor.graphics.endFill();
			//cursor.graphics.beginFill(0xFFFFFF, .5)
			cursor.graphics.drawEllipse(-erasersize/2, -erasersize/2, erasersize, erasersize);
			//cursor.graphics.endFill();
			cursor.graphics.lineStyle(2*unit,0x000000);
			cursor.graphics.moveTo(-erasersize/2*Math.sqrt(2)/2,-erasersize/2*Math.sqrt(2)/2);
			cursor.graphics.lineTo(erasersize/2*Math.sqrt(2)/2,erasersize/2*Math.sqrt(2)/2);
			cursor.graphics.moveTo(erasersize/2*Math.sqrt(2)/2,-erasersize/2*Math.sqrt(2)/2);
			cursor.graphics.lineTo(-erasersize/2*Math.sqrt(2)/2,erasersize/2*Math.sqrt(2)/2);
			
			cursor.x = deleteb.width/2;
			cursor.y = deleteb.height/2+4*unit;
			deleteb.addChild(cursor);
			return deleteb;
		}
		
		private function drawSelectedOutline():void
		{
			if (selectedOutline == null)
			{
				selectedOutline = new Shape();
				this.addChild(selectedOutline);
			}
			//outside violet
			//selectedOutline.graphics.lineStyle(1,0x000000);
			selectedOutline.graphics.lineStyle(2*unit,0xFF44CC);
			selectedOutline.graphics.drawRect(-2*unit, -2*unit, BUTTON_WIDTH+4, BUTTON_WIDTH+4);
			// center yellow
			//selectedOutline.graphics.lineStyle(2,0xFFFF44);
			selectedOutline.graphics.lineStyle(2,0xFFFF00);
			selectedOutline.graphics.drawRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH);
			//inside black
			//selectedOutline.graphics.lineStyle(1,0x000000);
			selectedOutline.graphics.lineStyle(2*unit,0xFF44CC);
			selectedOutline.graphics.drawRect(2*unit, 2*unit, BUTTON_WIDTH-4, BUTTON_WIDTH-4);			
			
		}
	}
}